﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Linq;
using System.Text;
using System.Windows;
using Vtron.HipcConfigurationTools.Core;
using Vtron.HipcConfigurationTools.Domain;

namespace Vtron.HipcConfigurationTools.VWASStyle
{
    /// <summary>
    /// ShowSetDevNameWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SetDevNameWindow : ModernWindow
    {
        public SetDevNameWindow(string devName)
        {
            InitializeComponent();
            this.Style = (Style)App.Current.Resources["EmptyWindow"];
            devName_TextBox.Text = devName;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            SocketHandler handler = new SocketHandler();
            T_HIPC_SYS_DEVNAME_REQ request = new T_HIPC_SYS_DEVNAME_REQ();
            request.head = new T_HIPC_NET_HEADER() { msgType = 0x841c };
            request.devName = Encoding.Default.GetBytes(devName_TextBox.Text).Concat(new byte[12 - devName_TextBox.Text.Length]).ToArray();
            try
            {
                T_HIPC_SYS_DEVNAME_RESP response = handler.Handle<T_HIPC_SYS_DEVNAME_RESP>(request);

                if (response.head.errCode == 0)
                {
                    ModernDialog.ShowMessage("设备名设置成功", "信息", MessageBoxButton.OK);
                    Close();
                }
                else
                {
                    ModernDialog.ShowMessage(String.Format("设备名设置失败！错误代码：", response.head.errCode), "错误", MessageBoxButton.OK);
                }
            }
            catch (Exception ex)
            {
                ModernDialog.ShowMessage(String.Format("设备名设置失败！错误信息：", ex.Message), "错误", MessageBoxButton.OK);
            }
        }
    }
}
